﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Chat_D_LuX
{
    public partial class frmRegister : Form
    {
        // Service reference
        ChatServiceRef.Chat_ServiceClient sr = new ChatServiceRef.Chat_ServiceClient();

        public frmRegister()
        {
            InitializeComponent();
        }

        private void btnRegister_Click(object sender, EventArgs e)
        {
            //Local variables
            //bool RegSuccess = false;
            bool isValid = false;
            string RegUser = txtRegUser.Text;
            string RegPass = txtRegPass.Text;
            string RegPassCon = txtRegConfirmPass.Text;
            string RegEmail = txtRegEmail.Text;

            isValid = isValidated(isValid, RegPass, RegPassCon);

            if (isValid == false)
            {
                this.Hide();
                const string message =
                    " The Passwords entered do not match. Please try again";
                const string caption =
                    "Password Mismatch";
                var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    txtRegConfirmPass.Text = "";
                    txtRegPass.Text = "";
                    this.Show();
                    txtRegPass.Focus();
                }

                if (result == System.Windows.Forms.DialogResult.No)
                {
                    Application.Exit();
                }
            }

            if (isValid == true)
            {                
                // Get username, pasword, and email from from
                string un = txtRegUser.Text.ToString();
                string pw = txtRegPass.Text.ToString();
                string email = txtRegEmail.Text.ToString();

                // Invoke service register function
                sr.Register(un, pw, email);
                

                //Service References being updated

                ////call to service refrence 'Client'
                //ClientRefrence.IService1 CRegister = new ClientRefrence.Service1Client();
                //RegSuccess = CRegister.Register(RegUser, RegPass, RegEmail, RegSuccess);

                //if (RegSuccess == true)
                //{
                //    this.Close();
                //}

                //else if (RegSuccess == false)
                //{
                //    Application.Exit();
                //}
            }
        }

        public bool isValidated(bool isValid, string pass, string conPass)
        {
            if (pass == conPass)
            {
                isValid = true;
            }

            else if (pass != conPass)
            {
                isValid = false;
            }
            return isValid;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtRegConfirmPass.Text = "";
            txtRegEmail.Text = "";
            txtRegPass.Text = "";
            txtRegUser.Text = "";
            txtRegUser.Focus();
        }

        private void txtRegUser_Leave(object sender, EventArgs e)
        {
            string un = txtRegUser.Text.ToString();
            lblUserValidate.Visible = false;
            if (sr.UserExists(un))
            {
                lblUserValidate.Text = "Username already in use.";
                lblUserValidate.Visible = true;
                txtRegUser.Focus();

            }
        }

        private void txtRegEmail_Leave(object sender, EventArgs e)
        {
            string email = txtRegEmail.Text.ToString();
            lblEmailValidate.Visible = false;
            if (sr.EmailExists(email))
            {
                lblEmailValidate.Text = "Email already in use.";
                lblEmailValidate.Visible = true;
                txtRegEmail.Focus();
            }
        }
    }
}
